package com.ruoyi.system.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.RewardsPunishmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EmployeeMapper;
import com.ruoyi.system.domain.Employee;
import com.ruoyi.system.service.IEmployeeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 员工管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-11
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService 
{
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RewardsPunishmentMapper rewardsPunishmentMapper;

    /**
     * 查询员工管理
     * 
     * @param employeeId 员工管理主键
     * @return 员工管理
     */
    @Override
    public Employee selectEmployeeByEmployeeId(Long employeeId)
    {
        return employeeMapper.selectEmployeeByEmployeeId(employeeId);
    }

    /**
     * 查询员工管理列表
     * 
     * @param employee 员工管理
     * @return 员工管理
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<Employee> selectEmployeeList(Employee employee)
    {
        return employeeMapper.selectEmployeeList(employee);
    }

    /**
     * 新增员工管理
     * 
     * @param employee 员工管理
     * @return 结果
     */
    @Override
    public int insertEmployee(Employee employee)
    {
        employee.setCreateTime(DateUtils.getNowDate());
        return employeeMapper.insertEmployee(employee);
    }

    /**
     * 修改员工管理
     * 
     * @param employee 员工管理
     * @return 结果
     */
    @Override
    public int updateEmployee(Employee employee)
    {
        employee.setUpdateTime(DateUtils.getNowDate());
        return employeeMapper.updateEmployee(employee);
    }

    /**
     * 批量删除员工管理
     * 
     * @param employeeIds 需要删除的员工管理主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeByEmployeeIds(String employeeIds)
    {
        return employeeMapper.deleteEmployeeByEmployeeIds(Convert.toStrArray(employeeIds));
    }

    /**
     * 删除员工管理信息
     * 
     * @param employeeId 员工管理主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeByEmployeeId(Long employeeId)
    {
        return employeeMapper.deleteEmployeeByEmployeeId(employeeId);
    }

    /**
     * 部门调动
     * @param employeeId 员工编号
     * @param dept 调入部门
     * @return 结果
     */
    @Override
    public int transferDeptByEmployeeId(long employeeId,String dept)
    {
        return employeeMapper.updateDeptByEmployeeId(employeeId,dept);
    }

    /**
     * 岗位调动
     * @param employeeId 员工编号
     * @param post 调入岗位
     * @return 结果
     */
    @Override
    public int transferPostByEmployeeId(long employeeId, String post) {
        return employeeMapper.updatePostByEmployeeId(employeeId,post);
    }

    /**
     * 转正
     * @param employeeId 员工编号
     * @return 结果
     */
    @Override
    public int TraineeToEmployee(long employeeId) {
//        Employee employee=employeeMapper.selectEmployeeByEmployeeId(employeeId);
//        String oldEmployeeName=employee.getEmployeeName();
//        int index=oldEmployeeName.indexOf("(实习)");
//        String newEmployeeName=oldEmployeeName.substring(0,index);
//        employee.setEmployeeName(newEmployeeName);
        return employeeMapper.TraineeToEmployee(employeeId);
    }

    /**
     * 上传档案
     * @param employeeId 员工编号
     * @param employeeFiles 档案文件名
     */
    @Override
    public void updateFilesByEmployeeId(long employeeId, String employeeFiles) {
        employeeMapper.updateFilesByEmployeeId(employeeId, employeeFiles);
    }


    /**
     * 统计部门人数
     * @param dept 部门
     * @return 结果
     */
    @Override
    public Integer countEmployeeByDept(String dept) {
        return employeeMapper.countEmployeeByDept(dept);
    }

    /**
     * 统计岗位人数
     * @param post 岗位
     * @return 结果
     */
    @Override
    public Integer countEmployeeByPost(String post) {
        return employeeMapper.countEmployeeByPost(post);
    }

    /**
     * 部门平均工资
     * @param dept 部门
     * @return 结果
     */
    @Override
    public Double avgSalaryByDept(String dept) {
        return employeeMapper.avgSalaryByDept(dept);
    }

    /**
     * 部门岗位工资
     * @param post 岗位
     * @return 结果
     */
    @Override
    public Double avgSalaryByPost(String post) {
        return employeeMapper.avgSalaryByPost(post);
    }

    /**
     * 部门全勤数
     * @param dept 部门
     * @return 结果
     */
    @Override
    public Double countAttendanceByDept(String dept) {
        return employeeMapper.countAttendanceByDept(dept);
    }

    /**
     * 岗位全勤数
     * @param post 岗位
     * @return 结果
     */
    @Override
    public Double countAttendanceByPost(String post) {
        return employeeMapper.countAttendanceByPost(post);
    }

    @Override
    public Double countLateByDept(String dept) {
        return employeeMapper.countLateByDept(dept);
    }

    @Override
    public Double countLateByPost(String post) {
        return employeeMapper.countLateByPost(post);
    }

    @Override
    public int checkSign(Long employeeId) {
        Employee employee = employeeMapper.selectEmployeeByEmployeeId(employeeId);
        Date leavetime = employee.getLeaveTime();
        Date backtime = employee.getBackTime();
        Date signtime = new Date();
        employeeMapper.setSignTime(employeeId);
        Calendar calendarsign = Calendar.getInstance();
        Calendar calendarleave = Calendar.getInstance();
        Calendar calendarback = Calendar.getInstance();
        calendarsign.setTime(signtime);
        int weekday = calendarsign.get(Calendar.DAY_OF_WEEK);
        if(weekday >= 2 && weekday <= 7) weekday--;
        else weekday = 7;
        int hour = calendarsign.get(Calendar.HOUR_OF_DAY);
        if(leavetime == null && backtime == null){//无请假
            if (weekday <= 5) {//周内
                if (hour < 9) {//未超过九点,不迟到
                    employeeMapper.setSign(employeeId);
                    return 1;
                } else if (hour < 14) {
                    rewardsPunishmentMapper.latePlus(employeeId);//迟到
                    employeeMapper.setSign(employeeId);
                    return 2;
                } else {
                    rewardsPunishmentMapper.cancelAttendance(employeeId);//缺勤
                    rewardsPunishmentMapper.latePlus(employeeId);//迟到
                    employeeMapper.setSign(employeeId);
                    return 3;
                }
            }
        }
        else {
            calendarleave.setTime(leavetime);
            calendarback.setTime(backtime);
        }
        int yearleave = calendarleave.get(Calendar.YEAR);
        int yearback = calendarback.get(Calendar.YEAR);
        int yearcount = yearback - yearleave;
        int yearsign = calendarsign.get(Calendar.YEAR);
        int yearleaveday = calendarleave.get(Calendar.DAY_OF_YEAR);
        int yearbackday = calendarback.get(Calendar.DAY_OF_YEAR);
        int yearsignday = calendarsign.get(Calendar.DAY_OF_YEAR);



        if(yearcount == 0) {//请假在同一年内回来
            if (yearleave == yearsign) {//签到日也在这一年
                if (yearleaveday < yearsignday && yearbackday > yearsignday) {//请假日
                    employeeMapper.setSign(employeeId);
                    Calendar ontime = Calendar.getInstance();
                    ontime.set(2000, Calendar.JANUARY, 1, 8, 0);
                    Date ontimes = ontime.getTime();
                    employee.setSignTime(ontimes);
                    employeeMapper.updateEmployee(employee);
                    return 4;//您已请假
                } else {//正常签到
                    if (weekday <= 5) {//周内
                        if (hour < 9) {//未超过九点,不迟到
                            employeeMapper.setSign(employeeId);
                            return 1;
                        } else if (hour < 14) {
                            rewardsPunishmentMapper.latePlus(employeeId);//迟到
                            employeeMapper.setSign(employeeId);
                            return 2;
                        } else {
                            rewardsPunishmentMapper.cancelAttendance(employeeId);//缺勤
                            rewardsPunishmentMapper.latePlus(employeeId);//迟到
                            employeeMapper.setSign(employeeId);
                            return 3;
                        }
                    }
                }
            }
            else {//请假日全部在下一年，正常签到
                if (weekday <= 5) {//周内
                    if (hour < 9) {//未超过九点,不迟到
                        employeeMapper.setSign(employeeId);
                        return 1;
                    } else if (hour < 14) {
                        rewardsPunishmentMapper.latePlus(employeeId);//迟到
                        employeeMapper.setSign(employeeId);
                        return 2;
                    } else {
                        rewardsPunishmentMapper.cancelAttendance(employeeId);//缺勤
                        rewardsPunishmentMapper.latePlus(employeeId);//迟到
                        employeeMapper.setSign(employeeId);
                        return 3;
                    }
                }
            }
        }
        else {//跨年请假
            if(yearleaveday > yearsignday){//未到请假日，正常签到
                if (weekday <= 5) {//周内
                    if (hour < 9) {//未超过九点,不迟到
                        employeeMapper.setSign(employeeId);
                        return 1;
                    } else if (hour < 14) {
                        rewardsPunishmentMapper.latePlus(employeeId);//迟到
                        employeeMapper.setSign(employeeId);
                        return 2;
                    } else {
                        rewardsPunishmentMapper.cancelAttendance(employeeId);//缺勤
                        rewardsPunishmentMapper.latePlus(employeeId);//迟到
                        employeeMapper.setSign(employeeId);
                        return 3;
                    }
                }
            }
            else if(yearleaveday < yearsignday || yearsignday < yearbackday) {//请假日
                employeeMapper.setSign(employeeId);
                Calendar ontime = Calendar.getInstance();
                ontime.set(2000, Calendar.JANUARY, 1, 8, 0);
                Date ontimes = ontime.getTime();
                employee.setSignTime(ontimes);
                employeeMapper.updateEmployee(employee);
                return 4;//您已请假
            }
            else if(yearsignday > yearbackday){//已过请假日，正常签到
                if (weekday <= 5) {//周内
                    if (hour < 9) {//未超过九点,不迟到
                        employeeMapper.setSign(employeeId);
                        return 1;
                    } else if (hour < 14) {
                        rewardsPunishmentMapper.latePlus(employeeId);//迟到
                        employeeMapper.setSign(employeeId);
                        return 2;
                    } else {
                        rewardsPunishmentMapper.cancelAttendance(employeeId);//缺勤
                        rewardsPunishmentMapper.latePlus(employeeId);//迟到
                        employeeMapper.setSign(employeeId);
                        return 3;
                    }
                }
            }
        }
        return 0;

    }

    @Override
    @Scheduled(cron = "0 0 8 ? * MON-FRI")//0 0 8 ? * MON-FRI
    public void checkLeave() {
        List<Long> allEmployeeId = employeeMapper.selectAllEmployeeId();
        for (Long employeeId: allEmployeeId){
            Employee employee = employeeMapper.selectEmployeeByEmployeeId(employeeId);
            Date leavetime = employee.getLeaveTime();
            Date backtime = employee.getBackTime();
            Date signtime = new Date();
//            employeeMapper.setSignTime(employeeId);
            Calendar calendarsign = Calendar.getInstance();
            Calendar calendarleave = Calendar.getInstance();
            Calendar calendarback = Calendar.getInstance();
            if(leavetime == null && backtime == null){
                continue;
            }
            calendarleave.setTime(leavetime);
            calendarback.setTime(backtime);
            calendarsign.setTime(signtime);
            int yearleave = calendarleave.get(Calendar.YEAR);
            int yearback = calendarback.get(Calendar.YEAR);
            int yearcount = yearback - yearleave;
            int yearsign = calendarsign.get(Calendar.YEAR);
            int yearleaveday = calendarleave.get(Calendar.DAY_OF_YEAR);
            int yearbackday = calendarback.get(Calendar.DAY_OF_YEAR);
            int yearsignday = calendarsign.get(Calendar.DAY_OF_YEAR);




            if(yearcount == 0) {//请假在同一年内回来
                if (yearleave == yearsign) {//签到日也在这一年
                    if (yearleaveday < yearsignday && yearbackday > yearsignday) {//请假日
                        employeeMapper.setSign(employeeId);
                        Calendar ontime = Calendar.getInstance();
                        ontime.set(2000, Calendar.JANUARY, 1, 8, 0);
                        Date ontimes = ontime.getTime();
                        employee.setSignTime(ontimes);
                        employeeMapper.updateEmployee(employee);
                    }
                }
            }
            else {//跨年请假
                if(yearleaveday < yearsignday || yearsignday < yearbackday) {//请假日
                    employeeMapper.setSign(employeeId);
                    Calendar ontime = Calendar.getInstance();
                    ontime.set(2000, Calendar.JANUARY, 1, 8, 0);
                    Date ontimes = ontime.getTime();
                    employee.setSignTime(ontimes);
                    employeeMapper.updateEmployee(employee);
                }
            }
        }
    }

    @Override
    @Scheduled(cron = "0 0 14 ? * MON-FRI")
    public void checkAttendance() {//周内下午两点检查是否有人尚未签到，有则取消全勤；检查完后清除当天打卡信息
        List<Long> allEmployeeId = employeeMapper.selectAllEmployeeId();
        for (Long employeeId: allEmployeeId){
            Employee employee = employeeMapper.selectEmployeeByEmployeeId(employeeId);
            int sign = employeeMapper.getSign(employeeId);
            if(sign == 0){//清除前还没签到，则为缺勤
                Date latetime = new Date();
                employee.setSignTime(latetime);
                employeeMapper.updateEmployee(employee);
                rewardsPunishmentMapper.cancelAttendance(employeeId);
                rewardsPunishmentMapper.latePlus(employeeId);
            }
        }
    }

    @Override
    @Scheduled(cron = "0 0 23 * * ?")
    public void resetSign() {
        employeeMapper.resetSign();
    }

    @Override
    public int setLeaveBackTime(Long employeeId, Date leaveTime, Date backTime) {
        return employeeMapper.setLeaveBackTime(employeeId, leaveTime, backTime);
    }


}
